package com.lcj.mis.service.impl;

import cn.hutool.core.map.MapUtil;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.lcj.common.util.CosUtil;
import com.lcj.common.util.Result;
import com.lcj.mis.controller.form.DriverBaseInfoForm;
import com.lcj.mis.controller.form.SearchDriverByPageForm;
import com.lcj.mis.controller.form.SendPrivateMessageForm;
import com.lcj.mis.controller.form.UpdateDriverRealAuthForm;
import com.lcj.mis.feign.DrServiceApi;
import com.lcj.mis.feign.SnmServiceApi;
import com.lcj.mis.service.DriverService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * @author lcj
 */
@Service
public class DriverServiceImpl implements DriverService {
    @Resource
    private DrServiceApi drServiceApi;

    @Resource
    private CosUtil cosUtil;

    @Resource
    private SnmServiceApi snmServiceApi;
    
    @Override
    public Result searchDriverByPage(SearchDriverByPageForm form) {
        Result result = drServiceApi.searchDriverByPage(form);
        return result;
    }

    @Override
    public HashMap searchDriverComprehensiveData(Long driverId) {
        HashMap map = new HashMap<>();
        HashMap realAuthMap = (HashMap) drServiceApi.searchDriverIsRealAuth(driverId).get("result");
        Integer realAuth = MapUtil.getInt(realAuthMap,"realAuthCode");
        if(realAuth.equals(2) || realAuth.equals(3)){
            Result r = drServiceApi.searchDriverRealSummary(new DriverBaseInfoForm(driverId));
            HashMap summaryMap = (HashMap) r.get("result");
            summaryMap.replace("idcardFront",cosUtil.getPrivateFileUrl(MapUtil.getStr(summaryMap, "idcardFront")));
            summaryMap.replace("idcardBack",cosUtil.getPrivateFileUrl(MapUtil.getStr(summaryMap, "idcardBack")));
            summaryMap.replace("idcardHolding",cosUtil.getPrivateFileUrl(MapUtil.getStr(summaryMap, "idcardHolding")));
            summaryMap.replace("drcardFront",cosUtil.getPrivateFileUrl(MapUtil.getStr(summaryMap, "drcardFront")));
            summaryMap.replace("drcardBack",cosUtil.getPrivateFileUrl(MapUtil.getStr(summaryMap, "drcardBack")));
            summaryMap.replace("drcardHolding",cosUtil.getPrivateFileUrl(MapUtil.getStr(summaryMap, "drcardHolding")));
            map.put("summaryMap", summaryMap);
        }
        return map;
    }

    @Override
    @Transactional
    @LcnTransaction
    public int updateDriverRealAuth(UpdateDriverRealAuthForm form) {
        Result r = drServiceApi.updateDriverRealAuth(form);
        int rows = MapUtil.getInt(r, "rows");
        //发送通知消息
        SendPrivateMessageForm messageForm = new SendPrivateMessageForm();
        messageForm.setReceiverIdentity("driver");
        messageForm.setReceiverId(form.getDriverId());
        //messageForm.setTtl(30*24 * 3600 * 1000);
        messageForm.setSenderId(0L);
        messageForm.setSenderIdentity("system");
        messageForm.setSenderName("随行打车");
        if(form.getRealAuth() == 2) {
            messageForm.setMsg("【审核提醒】您的实名认证审核通过！可以接单啦！");
        } else {
            messageForm.setMsg("【审核提醒】您的实名认证审核未通过！请核对后重新提交实名认证资料");
        }

        snmServiceApi.sendPrivateMessageSync(messageForm);
        return rows;
    }
}